/*
 * Copyright 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sychip.fhc

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateIntAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.fhc.base.isTablet
import com.fhc.view.text.ScaleInAnimatedText
import com.fhc.view.text.rememberAnimatedTextState
import com.fhc.view.utils.SvgIcon
import com.sychip.fhc.app.todo.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay

@Composable
fun AppCoverScreen(onShowCoverChange: (Boolean) -> Unit? = {}) {
    var showCover by remember { mutableStateOf(true) }
    LaunchedEffect(Unit) {
        delay(2600) // AppCoverContent内部显示动画时长大概isAnim2的delay +  zoomInImg2等等的durationMillis +100 ms
        showCover = false
        delay(1000) // AnimatedVisibility exit需要1000
        onShowCoverChange(showCover)
    }
    AnimatedVisibility(
        visible = showCover,
        enter = fadeIn(initialAlpha = 0f),
        exit = fadeOut(
            targetAlpha = 0f, animationSpec = tween(durationMillis = 1000) // 1秒淡出时长
        )
    ) {
        AppCoverContent()
    }
}

@Composable
fun AppCoverContent() {

    val isAnim2 = remember { mutableStateOf(true) }

    val stateScale = rememberAnimatedTextState()
    LaunchedEffect(Dispatchers.IO) {
        delay(400L)
        stateScale.start()
    }

    // 0.15*n秒后开始logo等动画的变化
    LaunchedEffect(true) {
        delay(2000L)
        isAnim2.value = false
    }

    val zoomOutImg by animateDpAsState(
        targetValue = if (isAnim2.value) if (isTablet()) 280.dp else 180.dp else 0.dp,
        animationSpec = tween(durationMillis = 500)
    )

    val spTxt by animateIntAsState(targetValue = if (isAnim2.value) if (isTablet()) 40 else 25 else 0, animationSpec = tween(durationMillis = 500))

    val zoomInImgLogo by animateDpAsState(
        targetValue = if (isAnim2.value) 0.dp else if (isTablet()) 280.dp else 180.dp,
        animationSpec = tween(durationMillis = 500)
    )
    val zoomInImgBottom by animateDpAsState(
        targetValue = if (isAnim2.value) 0.dp else if (isTablet()) 120.dp else 80.dp,
        animationSpec = tween(durationMillis = 500)
    )

    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomCenter) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White),
            verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            SvgIcon(
                path = "svg/logo.svg",
                contentDescription = null,
                modifier = Modifier.size(zoomInImgLogo),
            )
            Image(
                painter = painterResource(id = R.drawable.murata_bike),
                contentDescription = null,
                modifier = Modifier
                    .size(zoomOutImg)
                    .offset(x = zoomOutImg / 6), // 图片非主体居中，偏移估测大概量
            )

            ScaleInAnimatedText(
                state = stateScale,
                text = APP.TITLE,
                style = MaterialTheme.typography.titleLarge.copy(
                    fontWeight = FontWeight.Black,
                    fontSize = spTxt.sp, color = colorScheme.primary, textAlign = TextAlign.Center,
                ),
                animateOnMount = false
            )
        }

        Image(
            painter = painterResource(id = R.drawable.sychip_logo),
            contentDescription = null,
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(bottom = 50.dp)
                .size(zoomInImgBottom), // 图片非主体居中，偏移估测大概量
        )
    }
}
